<?php 
/**
 *
 * UCLibNewTip class file
 *
 * @category   UC
 * @author     刘海涛 <liuhaitao01@snda.com>
 * @copyright  SDO Group
 * @version    Release: 1.0.0
 * @link       
 * @license    http://www.sdo.com
 */

require_once(dirname(__FILE__).'/UCLibCache.php');

class UCLibNewTip
{	
	const MEMCACHE_KEY_FANS = 'sdo_uc_home_key_new_fans_';				//新增粉丝
	const MEMCACHE_KEY_REPLY = 'sdo_uc_home_key_new_reply_';			//新增评论或者回复
	const MEMCACHE_KEY_COMMENT = 'sdo_uc_home_key_new_comment_';		//新增留言
	const MEMCACHE_CHANNEL = 'home';
	const ERROR_MEMCACHE_PARAM = -1;
	const ERROR_MEMCACHE_SET = -2;
	const ERROR_MEMCACHE_DELETE = -3;
	private $_error_no = null;
	private $_uid = null;

	/**
	 * 构造函数
	 *
	 */
	function __construct($uid)
	{
		if(empty($uid))
		{
			$this->error_no = self::MEDAL_ERROR_PARAM;
			return false;
		}
		$this->_uid = $uid;
	}
	
	/**
	 * 析构函数
	 *
	 */
	function __destruct()
	{
	}

	/**
	 * 获取错误号
	 *
	 */
	public function getError()
	{
		 return $this->_error_no;
	}

	public function add($key)
	{
		$o_cache = new UCLibCache(self::MEMCACHE_CHANNEL);
		$s_key	= $key.$this->_uid;

		$s_ret = $o_cache->read($s_key);
		if($s_ret)
		{
			$s_ret += 1;
		}
		else
		{
			$s_ret = 1;
		}
		if(false===$o_cache->write($s_key, $s_ret))
		{
			$this->_error_no = self::ERROR_MEMCACHE_SET;
			return false;
		}
		return true;
	}

	public function clear($key)
	{
		$o_cache = new UCLibCache(self::MEMCACHE_CHANNEL); 
		$s_key		= $key.$this->_uid;
		if(false===$o_cache->delete($s_key))
		{
			$this->_error_no = self::ERROR_MEMCACHE_DELETE;
			return false;
		}
		return true;
	}
	
	/**
	 * 输出首页通知条
	 *
	 */
	public function getHtml()
	{
		$o_cache = new UCLibCache(self::MEMCACHE_CHANNEL,false); 
		$s = '';

		if($s_ret=$o_cache->read(self::MEMCACHE_KEY_FANS.$this->_uid))
		{
			$s .= '<li class="fans"><a href="/relation/myfans"><strong>' . $s_ret .  '</strong>新粉丝</a></li>';
		}

		if($s_ret=$o_cache->read(self::MEMCACHE_KEY_COMMENT.$this->_uid))
		{
			$s .= '<li class="mes"><a href="/home/comment/' . $this->_uid . '"><strong>' . $s_ret . '</strong>新主页留言</a></li>';
		}
		if($s_ret=$o_cache->read(self::MEMCACHE_KEY_REPLY.$this->_uid))
		{
			$s .= '<li class="tuita"><a href="/t/comment/get"><strong>' . $s_ret . '</strong>新推他评论</a></li>';
		}

		if(!empty($s))
		{
			$s = '<div class="InfoMod"><ul>' . $s . '</ul><div class="InfoModB"></div></div>';
		}
		$o_cache->close();
		return $s;
	}

}

?>